#delimit ;
clear all;
set more off;
pause on;

/**************************************************************************************************/
/* ABOUT THIS FILE */
/**************************************************************************************************/

/*
PURPOSE
This file makes the database.
It should be run after appeals.do and before the analysis files (an_can.do and an_pos.do).
It merges the regional data (regional.dta) plus the coded posters data (appeals.dta).
It then makes the independent variables.

OUTPUT
This file outputs versions of the database with:
1. The poster (n=755) as the unit of analysis (db_pos.dta)
2. The candidate (n=573) as the unit of analysis (db_can.dta)
3. Posters that have nationalist and Islamic appeals excluded, and the candidate at the UOA (db_can_excl.dta)
*/

/**************************************************************************************************/
/* MERGE REGIONAL DATA ONTO POSTER DATA. UOA: POSTER (756) */
/**************************************************************************************************/

use  "dta/appeals.dta";
merge m:1 region using "dta/regional.dta", force;
drop _merge;

/**************************************************************************************************/
/* INDEPENDENT VARIABLES */
/**************************************************************************************************/

/* Islamist Candidate ----------------------------------------------------------------------------*/

/* Islamic Parties = 1 (includes Aceh parties) */
gen is_party = 0;
replace is_party = 1 if party == 27; // Partai Bintang Reformasi
replace is_party = 1 if party== 38; // Partai Kedadilan Sejahtera
replace is_party = 1 if party == 25; // Partai Bulan Bintang
replace is_party = 1 if party == 41; // Partai Persatuan Pembangunan
replace is_party = 1 if party == 2; // Partai Aceh Aman Seujahtera
replace is_party = 1 if party == 3; // Partai Bersatu Atjeh
replace is_party = 1 if party == 4; // Partai Daulat Atjeh
replace is_party = 1 if party == 19; // Partai Persatuan Nahdlatul Ummah Indonesia

/* Moderate parties: Partai Amanat Nasional, Partai Kebangkitan Bangsa, & Partai Kebangkitan Nasional Ulama */

/* Regional Party vars */
gen reg_party = 0;
replace reg_party = 1 if party == 2; // Partai Aceh Aman Seujahtera
replace reg_party = 1 if party == 3; // Partai Bersatu Atjeh
replace reg_party = 1 if party == 4; // Partai Daulat Atjeh

gen party_acehcomb = party;
replace party_acehcomb = 0 if reg_party == 1; // party list, setting all Aceh parties to #0

/* Muslim population -----------------------------------------------------------------------------*/

gen muslim_pop = islam / totpop_rel; // 2010 figure from religion, includes nonrespondents

/* Modern sector GRDP ----------------------------------------------------------------------------*/

gen modern_grdp = total_grdp - farmfish_grdp; // x $100,000s
gen modern_grdp_pct = modern_grdp / total_grdp; // % of GRDP from modern sector

/* Urbanization ----------------------------------------------------------------------------------*/

gen pop_tot = pop_urban + pop_rural;
gen pop_perurban = pop_urban / pop_tot;

/* Regional Legislature --------------------------------------------------------------------------*/

gen reg_leg = 0;
replace reg_leg = 1 if election == "leg_dis";
replace reg_leg = 1 if election == "leg_pro";

/* Female Candidate -----------------------------------------------------------------------------*/
gen female_can = 0;
replace female_can = 1 if gender == "female";

/* Java & Jakarta --------------------------------------------------------------------------------*/
gen java = 0;
destring prov_no, replace;
replace java = 1 if prov_no == 3100;
replace java = 1 if prov_no == 3200;
replace java = 1 if prov_no == 3300;
replace java = 1 if prov_no == 3400;

gen jakarta = 0;
replace jakarta = 1 if prov_no == 3100;

/* Population (logged) ---------------------------------------------------------------------------*/

gen pop_log	= log(pop_tot); // 2010 figure

/**************************************************************************************************/
/* FORMAT & SAVE UOA = POSTER */
/**************************************************************************************************/

/* List of Vars to Keep */
local vars
  can_id region party election prov_no con // IDs
  nat_pos islam_pos // DVs
  is_party reg_party party_acehcomb muslim_pop modern_grdp_pct pop_perurban reg_leg female_can pop_log totpop_rel java jakarta; // IVs

order `vars';
keep `vars';

/* ID Vars */
label variable can_id "Candidate ID number";
label variable region "Electoral district";
label variable party "Party Number";
label variable election "Election";
label variable prov_no "Province Number";
label variable totpop_rel "Population";
label variable con "Candidate residence";

/* Label DVs */
label variable nat_pos "Nationalist poster (1/0)";
label variable islam_pos "Islamist poster (1/0)";

/* Label IVs */
label variable is_party "Islamic party candidate (1/0)";
label variable reg_party "Regional Parties (1/0)";
label variable party_acehcomb "Parties (with Aceh Parties Combined)";
label variable muslim_pop "Muslim population (%)";
label variable modern_grdp_pct "Modern sector GRDP (%)";
label variable pop_perurban "Urban population (%)";
label variable reg_leg "Regional legislature (1/0)";
label variable female_can "Female candidate (1/0)";
label variable pop_log "Population (logged)";
label variable java "Java (1/0)";
label variable jakarta "Jakarta (1/0)";

save "dta/db_pos.dta", replace;

/**************************************************************************************************/
/* FORMAT & SAVE UOA = CANDIDATE */
/**************************************************************************************************/

preserve;

collapse
(first) region party election prov_no con // ID vars
(mean) nat_pos islam_pos // DVs
is_party reg_party party_acehcomb muslim_pop modern_grdp_pct pop_perurban reg_leg female_can pop_log totpop_rel java jakarta, // IVs
by(can_id);

/* ID Vars */
label variable can_id "Candidate ID number";
label variable region "Electoral district";
label variable party "Party Number";
label variable election "Election";
label variable prov_no "Province Number";
label variable totpop_rel "Population";
label variable con "Candidate residence";

/* Label DVs */
label variable nat_pos "Nationalist poster campaign (%)";
label variable islam_pos "Islamist poster campaign (%)";

/* Label IVs */
label variable is_party "Islamist party candidate (1/0)";
label variable reg_party "Regional Parties (1/0)";
label variable party_acehcomb "Parties (with Aceh Parties Combined)";
label variable muslim_pop "Muslim population (%)";
label variable modern_grdp_pct "Modern sector GRDP (%)";
label variable pop_perurban "Urban population (%)";
label variable reg_leg "Regional legislature (1/0)";
label variable female_can "Female candidate (1/0)";
label variable pop_log "Population (logged)";
label variable java "Java (1/0)";
label variable jakarta "Jakarta (1/0)";

save "dta/db_can.dta", replace;

/**************************************************************************************************/
/* FORMAT & SAVE UOA = CANDIDATE. Exclude posters with both nationalist and Islamic appeals */
/**************************************************************************************************/

restore;
drop if islam_pos ==1 & nat_pos == 1;

collapse
(first) region party election prov_no con // ID vars
(mean) nat_pos islam_pos // DVs
is_party reg_party party_acehcomb muslim_pop modern_grdp_pct pop_perurban reg_leg female_can pop_log totpop_rel java jakarta, // IVs
by(can_id);

/* ID Vars */
label variable can_id "Candidate ID number";
label variable region "Electoral district";
label variable party "Party Number";
label variable election "Election";
label variable prov_no "Province Number";
label variable totpop_rel "Population";
label variable con "Candidate residence";

/* Label DVs */
label variable nat_pos "Nationalist poster campaign (%)";
label variable islam_pos "Islamist poster campaign (%)";

/* Label IVs */
label variable is_party "Islamist party candidate (1/0)";
label variable reg_party "Regional Parties (1/0)";
label variable party_acehcomb "Parties (with Aceh Parties Combined)";
label variable muslim_pop "Muslim population (%)";
label variable modern_grdp_pct "Modern sector GRDP (%)";
label variable pop_perurban "Urban population (%)";
label variable reg_leg "Regional legislature (1/0)";
label variable female_can "Female candidate (1/0)";
label variable pop_log "Population (logged)";
label variable java "Java (1/0)";
label variable jakarta "Jakarta (1/0)";

save "dta/db_can_excl.dta", replace;

/* END ***********************************************************************************************/
